﻿
namespace Aeon.Emulator.Instructions.FPU
{
    internal static class Fxam
    {
        [Opcode("D9E5", OperandSize = 16 | 32, AddressSize = 16 | 32)]
        public static void Examine(VirtualMachine vm)
        {
            var fpu = vm.Processor.FPU;
            FPUStatus flags = fpu.StatusFlags & ~ConditionCodes.ConditionMask;

            if(!fpu.IsRegisterUsed(0))
                flags = Empty;
            else
            {
                double st0 = fpu.ST0;

                if(st0 == 0)
                    flags = Zero;
                else if(double.IsInfinity(st0))
                    flags = Infinity;
                else if(double.IsNaN(st0))
                    flags = NaN;
                else
                    flags = Normal;
            }

            fpu.StatusFlags = flags;
            vm.Processor.InstructionEpilog();
        }

        private const FPUStatus NaN = FPUStatus.C0;
        private const FPUStatus Normal = FPUStatus.C2;
        private const FPUStatus Infinity = FPUStatus.C0 | FPUStatus.C2;
        private const FPUStatus Zero = FPUStatus.C3;
        private const FPUStatus Empty = FPUStatus.C0 | FPUStatus.C3;
        private const FPUStatus Denormal = FPUStatus.C2 | FPUStatus.C3;
    }
}
